<?php

/**
 * Model_Post

 * @package    Model
 * @author     Windy
 */

class Model_Post extends Zend_Db_Table_Abstract
{
	protected $_name = "post";
	protected $_referenceMap = array(
		"SalesPerson" => array(
			"columns" 		=> "user_id",
			"refTableClass" => "Model_User",
			"refColumns"	=> "id"
		),
		"Category" => array(
			"columns"		=> "category_id",
			"refTableClass" => "Model_Category",
			"refColumns"	=> "id"
		)
	);
	/**
	 * 
	 * Tạo bài viết/ sản phẩm mới
	 * @param string $title
	 * @param string $summary
	 * @param string $content
	 * @param datetime $created_date
	 * @param int $user_id
	 * @param string $slug
	 * @param string $feature_image
	 * @param string $type
	 * @param int $category_id
	 * @param string $page_title Thẻ title của trang
	 * @param string $page_description Thẻ description của trang
	 * @param string $tag
	 * @return int Mã bài viết
	 */
	function insertPost($title, $summary, $content, $created_date, $user_id, $slug, $feature_image , $type, $page_title, $page_description, $tag, $price = 0, $featured = 0, $category_id = 0 ){
		try{
			$post = $this->createRow();
			$post->title = $title;
			$post->summary = $summary;
			$post->content = $content;
			$post->created_date = $created_date;
			$post->user_id = $user_id;
			$post->slug = $slug;
			$post->feature_image = $feature_image;
			$post->type = $type;
			$post->price = $price;
			$post->featured = $featured;
			$post->category_id = $category_id;
			$post->page_title = $page_title;
			$post->page_description = $page_description;
			$post->tag = $tag;
			$post->save();
			//$id = $this->_db->lastInsertId();
			return $post->id;
		
		}catch(Exception $ex){
			echo $ex->getMessage();
			return false;
		}
	}
/**
	 * 
	 * Update thông tin post
	 * @param int $id Mã post
	 * @param string $title Tiêu đề
	 * @param string $summary Tóm tắt
	 * @param string $content Nội dung
	 * @param string $slug Url hiển thị
	 * @param string $feature_image Ảnh đại diện
	 * @param int $category_id Mã chuyên mục
	 * @param string $page_title Thẻ title của trang web
	 * @param string $page_description Thẻ description của trang web
	 * @param string $tag Thẻ keyword cho bài viết
	 * @return int Số các bản ghi
	 */
	function updatePost($id, $title, $summary, $content, $slug, $feature_image, $page_title, $page_description, $tag, $user_id, $price = 0, $featured = 0, $category_id = 0){
		try{
			$result = $this->update(
									array(
										'title' 			=> $title,
										'summary' 			=> $summary,
										'content' 			=> $content,
										'slug'	 			=> $slug,
										'feature_image' 	=> $feature_image,
										'category_id'		=> $category_id,
										'price'				=> $price,
										'user_id'			=> $user_id,
										'featured'			=> $featured,
										'page_title'		=> $page_title,
										'page_description'		=> $page_description,
										'tag'				=> $tag
									),
									'id = '.$id
								);
			return true;
		}catch(Exception $ex){
			echo "Caught exception: ".$ex->getMessage();
			return false;
		}
	}
	/**
	 * Đếm số lượng post trong bảng
	 * @return int total-post
	 */
	function countPost(){
		 $select = $this->_db->select()->from('post',array("COUNT(post.id) as total"));
		 return $this->_db->fetchOne($select);
		
	}
	/**
	 * Đếm số lượng post theo type
	 * @param string $type
	 * @return int
	 */
	function countType($type){
		 $select = $this->_db->select()->from('post',array("COUNT(post.id) as total"))->where("type = '".$type."'");
		 return $this->_db->fetchOne($select);
	}
	/**
	 * Đếm số lượng post có $id_category nào đó
	 * @param int $id_category
	 * @return int $postInCategory
	 */
	function countPostInCategory($category_id){
		$select = $this->_db->select()->from('post',array("COUNT(post.id) as total"))->where("category_id = ".$category_id);
		 return $this->_db->fetchOne($select);
	}
	/**
	 * 
	 * Lấy toàn bộ các bài viết
	 * @return Zend_Db_Table_Rowset
	 */
	function getAllArticles(){
		return $this->fetchAll($this->select()->where('type = ?', 'article'));
	}
	/**
	 * 
	 * Lấy toàn bộ các trang
	 * @return Zend_Db_Table_Rowset
	 */
	function getAllPages(){
		return $this->fetchAll($this->select()->where('type = ?', 'page'));
	}
	/**
	 * 
	 * Lấy post theo type
	 * @param string $type
	 */
	function getPostsByType($type){
		return $this->fetchAll($this->select()->where('type = ?', $type)->order("created_date desc"));
	}
	/**
	 * Lấy post theo các điều kiện: type, title, category
	 * 
	 */
	public function getAllPosts($type = '', $title = '', $category_id = 0){
		try{
		$select = $this->select();
		
		if($type != '' ) $select = $select->where("type = ?", $type); 
		if($category_id != 0 ) $select = $select->where("category_id= ?", $category_id);
		if($title != '' ) $select->where("title like '%".$title."%' OR MATCH (title) AGAINST ('".$title."' IN BOOLEAN MODE) OR title LIKE '%".$title."%' OR MATCH (title) AGAINST ('".$title."' IN BOOLEAN MODE)");
		return $this->fetchAll($select);
		}catch(Exception $ex){
			echo $ex->getMessage();
			return null;
		}
	}
	/**
	 * 
	 * Lấy bài viết  theo id
	 * @param int $id
	 * @return Zend_Db_Table_Row
	 */
	function getPostByID($id){
		return $this->find($id)->current();
	}
	/**
	 * Lấy bài viết theo category_id
	 * @param int $category_id
       * @param string $type
       * @param int $number
	 * @return Zend_Db_Table_Rowset
	 */
	function  getPostByCategoryID($category_id, $type= "car", $number = 0, $featured = false)
	{
          $select = $this->select()->where('category_id = ?', $category_id)->where('type = ?', $type)->order("created_date desc");
          if($number && $number > 0){
               $select = $select->limit($number);
          }
          if($featured){
              $select = $select->where('featured = 1');
          }
            return $this->fetchAll($select);
	}
	/**
	 * 
	 * Lấy bài viết theo slug
	 * @param string $slug
	 * @return Zend_Db_Table_Row
	 */
	function getPostBySlug($slug){
		return $this->fetchRow($this->select()->where('slug = ?',$slug));
	}
	/**
	 * 
	 * Lấy các bài viết liên quan tới bài viết hiện tại
	 * @param Zend_Db_Table_Row $post Bài post hiện tại
	 * @param int $num Số bài viết liên quan muốn lấy
	 */
	function getRelatePosts($post, $num = 10){
		return $this->fetchAll($this->select()->where('category_id =?', $post->category_id)->where('id <> ?', $post->id)->limit($num));
	}
	/**
	 * 
	 * Lấy các sản phẩm được đánh dấu nổi bật theo chủng loại
	 * @param unknown_type $type
	 */
	function getFeatured($type, $number = 0){
                $select = $this->select()->where('type = ?', $type)->where('featured = 1')->order('created_date desc');
                if($number != 0) $select = $select->limit ($number);
		return $this->fetchAll($select);
	}
	
	/**
	 * 
	 * Xóa post
	 * @param int $id
	 */
	function getLastestPost($type, $numOfPosts){
		return $this->fetchAll($this->select()->where('type = ?', $type)->order("created_date desc")->limit($numOfPosts));
	}
	function deletePost($id){
		try{
			if(is_numeric($id)){
				return $this->delete('id = '. $id );
			}else{
				return $this->delete('id in ('.$id.')');
			}
		}catch (Exception $ex){
			echo $ex->getMessage();
			return false;	
		}
	}
      function searchPosts($key, $category_id = 0, $type = ''){
          try{
              $select = $this->select();
                if(isset($key) && $key != ""){
                    $select = $select->where("MATCH (title) AGAINST ('".$key."' IN BOOLEAN MODE) OR title LIKE '%".$key."%'");
                }
                if(isset($category_id) && $category_id != 0){
                    $select = $select->where("category_id = ?", $category_id);
                }
                if(isset($type) && $type != ''){
                    $select = $select->where("type = ?", $type);
                }
                $select->order("type desc")->order("category_id asc");
                return $this->fetchAll($select);
          }catch(Exception $ex){
              echo $ex->getTraceAsString();
          }
      }
}